package com.yj.event_listener.contentObserver;

import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.CalendarContract;

import androidx.annotation.Nullable;

import com.yj.event_listener.interfaces.NotificationCallback;
import com.yj.event_listener.util.CalendarUtil;
import com.yj.event_listener.util.LogUtil;
import com.yj.event_listener.util.Utils;

import java.text.SimpleDateFormat;
import java.util.Date;

public class CalendarContentObserver extends ContentObserver {

    private final Context context;
    private final NotificationCallback notificationCallback;

    /**
     * Creates a content observer.
     *
     * @param handler The handler to run {@link #onChange} on, or null if none.
     */
    public CalendarContentObserver(Handler handler, Context context, NotificationCallback callback) {
        super(handler);
        this.context = context;
        this.notificationCallback = callback;
    }

    @Override
    public void onChange(boolean selfChange) {
        LogUtil.d("onChange");
        onChange(selfChange, null);
    }

    private Long lastChange = 0L;
    private final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
    private final SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd HH:mm");
    @Override
    public void onChange(boolean selfChange, @Nullable Uri uri) {
        long current = System.currentTimeMillis();
        LogUtil.d("onChange "+uri + " lastChange " + lastChange + " current " + current);
        if(current - lastChange < 10000){
            return;
        }
        lastChange = current;

//        super.onChange(selfChange, uri);
        Cursor cursor = CalendarUtil.getCalendarAlertContent(context, lastChange);
        // Process the cursor to get calendar alert content
        if (cursor != null && cursor.moveToFirst()) {
            do {
                // Retrieve alert content
                long alertId = cursor.getLong(cursor.getColumnIndex(CalendarContract.CalendarAlerts._ID));
                String title = cursor.getString(cursor.getColumnIndex(CalendarContract.CalendarAlerts.TITLE));
                String location = cursor.getString(cursor.getColumnIndex(CalendarContract.CalendarAlerts.EVENT_LOCATION));
                long alarmTime = cursor.getLong(cursor.getColumnIndex(CalendarContract.CalendarAlerts.ALARM_TIME));
                String description = cursor.getString(cursor.getColumnIndex(CalendarContract.CalendarAlerts.DESCRIPTION));
                int AVAILABILITY = cursor.getInt(cursor.getColumnIndex(CalendarContract.CalendarAlerts.AVAILABILITY));
                int STATUS = cursor.getInt(cursor.getColumnIndex(CalendarContract.CalendarAlerts.STATUS));
                int STATE = cursor.getInt(cursor.getColumnIndex(CalendarContract.CalendarAlerts.STATE));
                int HAS_ALARM = cursor.getInt(cursor.getColumnIndex(CalendarContract.CalendarAlerts.HAS_ALARM));
                long begin = cursor.getLong(cursor.getColumnIndex(CalendarContract.CalendarAlerts.BEGIN));

                // Log the alert content
                LogUtil.d("Calendar Alert ID: " + alertId);
                LogUtil.d("Title: " + title);
                LogUtil.d("Location: " + location);
                LogUtil.d("Alarm Time: " + alarmTime);
                LogUtil.d("Begin: " + begin);
                LogUtil.d("Description: " + description);
                LogUtil.d("AVAILABILITY: " + AVAILABILITY);
                LogUtil.d("STATUS: " + STATUS);
                LogUtil.d("STATE: " + STATE);
                LogUtil.d("HAS_ALARM: " + HAS_ALARM);

                if(notificationCallback != null){
                    Date beginTime = new Date(begin);
                    notificationCallback.onCalendar(
                            Utils.isToday(begin) ? timeFormat.format(beginTime) : dateFormat.format(beginTime),
                            title
                    );
                }

            } while (cursor.moveToNext());

            cursor.close();
        }
    }
}
